import React from 'react';
import Mixins from '@/components/Common/mixin';
import { Flag, DefaultTitle } from '@/pages/riskControl/deviantWithdrawReview/details/utils';
import connectFn from '@/utils/connectFn';
import constant from '@/const';
import {
  getFinanceData,
  getBalanceData,
  getFundsData,
  getMemberData,
  getCustomerData,
} from '@/pages/riskControl/deviantWithdrawReview/details/agentConfig';
import { StateType } from '@/common-typings';
import { types } from '@/utils/utils';
import _ from 'lodash';
import { formatText } from '@/components/CreateTable/utils';

const { SUCCESS_CODE } = constant;

@connectFn(
  [],
  [
    'getBalanceManageBalance',
    'getAgentManageTrendFund',
    'getUserRiskManageAgentFunds',
    'getUserRiskManageAgentUser',
    'getUserRiskManageAgentCustomer',
  ],
)
class Agent extends Mixins {
  state = {
    financeLoading: true,
    balanceLoading: true,
    fundsLoading: true,
    memberLoading: true,
    financeList: [],
    balanceList: [],
    fundsList: [],
    memberList: [],
    customerData: {},
  };

  componentDidMount(): void {
    this.requestAccountBalance();
    this.requestFinanceData();
    this.requestFundsData();
    this.requestMemberData();
    this.requestCustomerData();
  }

  /**
   * 财务信息
   */
  requestFinanceData = () => {
    const { getAgentManageTrendFund, config } = this.props;

    const params: StateType = { userId: config.userId };

    this.changePageLoading({ financeLoading: true });
    this.changeState({ financeList: [] });

    getAgentManageTrendFund(params).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data && types(data) === 'object') {
        this.changeState({ financeList: [data] });
      }

      this.changePageLoading({ financeLoading: false });
    });
  };

  /**
   * 获取账户余额
   */
  requestAccountBalance = () => {
    const { config, getBalanceManageBalance } = this.props;

    this.changePageLoading({ balanceLoading: true });
    // 防止数据缓存
    this.changeState({ balanceList: [] });

    getBalanceManageBalance({
      userId: config.userId,
    }).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data && types(data) === 'array') {
        const result: StateType = {
          operates: '余额',
        };
        const balance = _.find(data, { channelId: 0 });
        const freezeBalance = _.find(data, { channelId: 101 });

        result.balance = balance.balance || 0;
        result.freezeBalance = freezeBalance.balance || 0;

        this.changeState({ balanceList: [result] });
      }
      this.changePageLoading({ balanceLoading: false });
    });
  };

  /**
   * 资金概要
   */
  requestFundsData = () => {
    const { getUserRiskManageAgentFunds, config } = this.props;
    const date = this.initDefaultDateByMonths(3);

    const params: StateType = {
      userId: config.userId,
      startTime: formatText(date[0], { formatTimeStamp: true }),
      endTime: formatText(date[1], { formatTimeStamp: true }),
    };

    this.changePageLoading({ fundsLoading: true });
    this.changeState({ fundsList: [] });

    getUserRiskManageAgentFunds(params).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data && types(data) === 'array') {
        this.changeState({ fundsList: data || [] });
      }

      this.changePageLoading({ fundsLoading: false });
    });
  };

  /**
   * 代理人员概要
   */
  requestMemberData = () => {
    const { getUserRiskManageAgentUser, config } = this.props;
    const date = this.initDefaultDateByMonths(3);

    const params: StateType = {
      userId: config.userId,
      startTime: formatText(date[0], { formatTimeStamp: true }),
      endTime: formatText(date[1], { formatTimeStamp: true }),
    };

    this.changePageLoading({ memberLoading: true });
    this.changeState({ memberList: [] });

    getUserRiskManageAgentUser(params).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data && types(data) === 'array') {
        this.changeState({ memberList: data || [] });
      }

      this.changePageLoading({ memberLoading: false });
    });
  };

  /**
   * 大客户统计
   */
  requestCustomerData = () => {
    const { getUserRiskManageAgentCustomer, config } = this.props;

    const date = this.initDefaultDateByMonths(3);

    const params: StateType = {
      userId: config.userId,
      startTime: formatText(date[0], { formatTimeStamp: true }),
      endTime: formatText(date[1], { formatTimeStamp: true }),
    };

    this.changeState({ customerData: {} });
    getUserRiskManageAgentCustomer(params).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data && types(data) === 'object') {
        this.changeState({ customerData: data });
      }
    });
  };

  /**
   * 账户详情
   */
  renderAccountInfo = () => {
    const { config } = this.props;
    return (
      <div>
        <Flag>账户详情</Flag>
        <DefaultTitle parentAgent={config.parentAgent} username={config.username} />
        {getFinanceData(this)}
        {getBalanceData(this)}
      </div>
    );
  };

  /**
   * 资金概要
   */
  renderFundsInfo = () => (
    <div>
      <Flag>资金概要（只统计下级会员账户）</Flag>
      {getFundsData(this)}
    </div>
  );

  /**
   * 人员概要
   */
  renderMemberInfo = () => (
    <div>
      <Flag>人员概要</Flag>
      {getMemberData(this)}
    </div>
  );

  /**
   * 大客户统计
   */
  renderCustomerInfo = () => (
    <div>
      <Flag>大客户统计</Flag>
      {getCustomerData(this)}
    </div>
  );

  render() {
    return (
      <div>
        {this.renderAccountInfo()}
        {this.renderFundsInfo()}
        {this.renderMemberInfo()}
        {this.renderCustomerInfo()}
      </div>
    );
  }
}

export default Agent;
